package com.yilin.tms.order.controller;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.NoLoginSecurity;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.entity.order.goods.GoodsOrder;
import com.yilin.tms.core.entity.order.rob.RobOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.order.service.order.IGoodsService;
import com.yilin.tms.order.service.order.IRobService;
import com.yilin.tms.order.service.order.ITransitService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order/security/")
public class SecurityController extends BaseController {
    @Resource
    IGoodsService goodsService;
    @Resource
    IRobService robService;
    @Resource
    ITransitService transitService;

    /**
     * 一键下线用户所有货源
     */
    @PostMapping(value = "recallGoodsOrderByAccountId")
    @NotNull("accountId")
    @NoLoginSecurity
    @SkipLoginVerify
    public ReturnData<Object> recallGoodsOrderByAccountId(String accountId) {
        goodsService.recallGoodsOrderByAccountId(accountId);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 一键下线组织所有货源
     */
    @PostMapping(value = "recallGoodsOrderByOrganizeId")
    @NotNull("organizeId")
    @NoLoginSecurity
    @SkipLoginVerify
    public ReturnData<Object> recallGoodsOrderByOrganizeId(String organizeId) {
        goodsService.recallGoodsOrderByOrganizeId(organizeId);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 检查是否存在货源
     */
    @PostMapping(value = "checkExistRunGoodsByAccountId")
    @NotNull("accountId")
    @SkipLoginVerify
    public ReturnData<Boolean> checkExistRunGoodsByAccountId(@Explain("账户ID") String accountId) {
        List<GoodsOrder> goodsOrderList = goodsService.getRunGoodsOrderListByAccountId(accountId);
        return jsonView(SUCCESS_CODE, "获取结果成功", +goodsOrderList.size() > 0);
    }

    /**
     * 检查是否存在货源
     */
    @PostMapping(value = "checkExistRunGoodsByOrganizeId")
    @NotNull("accountId")
    @SkipLoginVerify
    public ReturnData<Boolean> checkExistRunGoodsByOrganizeId(@Explain("账户ID") String organizeId) {
        List<GoodsOrder> goodsOrderList = goodsService.getRunGoodsOrderListByAccountId(organizeId);
        return jsonView(SUCCESS_CODE, "获取结果成功", +goodsOrderList.size() > 0);
    }

    /**
     * 检查是否存在订单
     * 包括抢单+运单
     */
    @PostMapping(value = "checkExistRunOrderByAccountId")
    @NotNull("accountId")
    @SkipLoginVerify
    public ReturnData<Boolean> checkExistRunOrderByAccountId(@Explain("账户ID") String accountId) {
        List<RobOrder> robOrderList = robService.getRunRobOrderByAccountId(accountId);
        List<TransitOrder> transitOrderList = transitService.getRunTransitOrderByAccountId(accountId);
        return jsonView(SUCCESS_CODE, "获取结果成功", (robOrderList.size() + transitOrderList.size()) > 0);
    }

    /**
     * 检查是否存在订单
     * 包括抢单+运单
     */
    @PostMapping(value = "checkExistRunOrderByOrganizeId")
    @NotNull("organizeId")
    @SkipLoginVerify
    public ReturnData<Boolean> checkExistRunOrderByOrganizeId(@Explain("组织ID") String organizeId) {
        List<RobOrder> robOrderList = robService.getRunRobOrderByOrganizeId(organizeId);
        List<TransitOrder> transitOrderList = transitService.getRunTransitOrderByOrganizeId(organizeId);
        return jsonView(SUCCESS_CODE, "获取结果成功", robOrderList.size() + transitOrderList.size() > 0);
    }


}
